//
//  SalesOrderDetailTVC.m
//  POS
//
//  Created by sigma xu on 4/25/13.
//  Copyright (c) 2013 sigma xu. All rights reserved.
//

#import "SalesOrderDetailTVC.h"

@interface SalesOrderDetailTVC ()

@end

@implementation SalesOrderDetailTVC
@synthesize so_id,so_number,create_date,sales_man,total_amount,total_item,status,client,remark;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
}
-(void) load_so_detail{
    SalesOrderDBO *sodbo = [[SalesOrderDBO alloc] init];
    sodbo.so_id = so_id;
    sodbo = [sodbo get_sales_order_by_so_id];
    so_number = sodbo.so_number;
    client = sodbo.client;
    create_date = sodbo.create_date;
    sales_man = sodbo.sales_man;
    total_amount = sodbo.total_amount;
    total_item = sodbo.total_item;
    status = sodbo.status;
    remark = sodbo.remark;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 3;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section ==0) {
        return @"Sales Order Information";
    }
    return @"";
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int numberofrow = 1;
    if (section==0) {
        numberofrow = 8;
    }
    // Return the number of rows in the section.
    return numberofrow;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier;
    UITableViewCellStyle style = UITableViewCellStyleDefault;
    if (indexPath.section == 0) {
        CellIdentifier = @"SalesOrderDetailCell";
    }else{
        CellIdentifier = @"SalesOrderButtonDetailCell";
    }
    UITableViewCell *cell = [self createCellForIdentifier:CellIdentifier
                                                tableView:tableView
                                                indexPath:indexPath
                                                    style:style
                             ];
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn1 setTitle:@"View Item List" forState:UIControlStateNormal];
    btn1.frame = CGRectMake(0, 0,145,40);
    [btn1 addTarget:self action:@selector(view_item_list:) forControlEvents:UIControlEventTouchUpInside];
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn2 setTitle:@"Post to Server" forState:UIControlStateNormal];
    btn2.frame = CGRectMake(155, 0,145, 40);
    [btn2 addTarget:self action:@selector(post_to_server:) forControlEvents:UIControlEventTouchUpInside];
    UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn3 setTitle:@"Update this SO" forState:UIControlStateNormal];
    btn3.frame = CGRectMake(0, 0,145,40);
    [btn3 addTarget:self action:@selector(update_sales_order:) forControlEvents:UIControlEventTouchUpInside];
    UIButton *btn4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn4 setTitle:@"Update Remark" forState:UIControlStateNormal];
    btn4.frame = CGRectMake(155, 0,145, 40);
    [btn4 addTarget:self action:@selector(update_remark:) forControlEvents:UIControlEventTouchUpInside];
    switch (indexPath.section) {
        case 0:
            self.title = [self so_number];
            if(indexPath.row == 0){
                cell.textLabel.text = @"Client";
                cell.detailTextLabel.text = [self client];
            }else if(indexPath.row == 1){
                cell.textLabel.text = @"Create Date";
                cell.detailTextLabel.text = [self create_date];
            }else if(indexPath.row == 2){
                cell.textLabel.text = @"Sales Man";
                cell.detailTextLabel.text = [self sales_man];
            }else if(indexPath.row == 3){
                cell.textLabel.text = @"Total Amount";
                cell.detailTextLabel.text =[@"$ " stringByAppendingString:[self total_amount]];
            }else if(indexPath.row == 4){
                cell.textLabel.text = @"Total Item";
                cell.detailTextLabel.text = [self total_item] ;
            }else if(indexPath.row == 5){
                cell.textLabel.text = @"Status";
                if ([status isEqualToString:@"1"]) {
                    cell.detailTextLabel.text = @"Local";
                }else if([status isEqualToString:@"2"]){
                    cell.detailTextLabel.text = @"Posted";
                    
                }
            }else if(indexPath.row == 6){
                cell.textLabel.text = @"Remark";
                cell.detailTextLabel.text = [self remark];
            }else if(indexPath.row == 7){
                cell.textLabel.text = @"So_Number";
                cell.detailTextLabel.text = [self so_number];
            }
            break;
        case 1:
            //            cell.textLabel.text = @"View Detail of Each Item";
            [cell.contentView addSubview:btn1];
            [cell.contentView addSubview:btn2];
            break;
        case 2:
            //cell.textLabel.text = @"Post To Server";
            [cell.contentView addSubview:btn3];
            [cell.contentView addSubview:btn4];
            break;
    }
    if (indexPath.section != 0) {
        //        cell.textLabel.textAlignment = UITextAlignmentCenter;
        cell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
        [cell setBackgroundColor:[UIColor clearColor]];
        
    }
    return cell;
}
- (UITableViewCell *)createCellForIdentifier:(NSString *)CellIdentifier
                                   tableView:(UITableView *)tableView
                                   indexPath:(NSIndexPath *)indexPath
                                       style:(UITableViewCellStyle)style{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell==nil){
        cell = [[UITableViewCell alloc]initWithStyle:style reuseIdentifier:CellIdentifier];
    }
    return cell;
}
- (void)view_item_list:(id)sender{
    ItemDBO *idbo = [[ItemDBO alloc] init];
    ItemListTVC *iltvc = [[ItemListTVC alloc] initWithStyle:UITableViewStyleGrouped];
    iltvc.title = self.title;
    iltvc.item_list = [idbo load_item_list_from_so:self.so_id];
    [self.navigationController pushViewController:iltvc animated:YES];
}
- (void)post_to_server:(id)sender{
    if ([status isEqualToString:@"2"]) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @""
                                                       message: @"This Sales Order has already been posted to server"
                                                      delegate: self
                                             cancelButtonTitle:@"Cancel"
                                             otherButtonTitles:@"Re-Post",nil];
        alert.tag = 1;
        [alert show];
    }else{
        NSLog(@"post to server");
        ServiceConnector *sc = [[ServiceConnector alloc] init];
       // sc.delegate = self;
        [sc post_sales_order:so_id];
    }
}
- (void)update_sales_order:(id)sender{
    
    UINavigationController *nav = [self.tabBarController.viewControllers objectAtIndex:0];
    ScanVC *svc = (ScanVC *)nav.topViewController;
    
    SalesOrderDBO *sodbo = [[SalesOrderDBO alloc] init];
    sodbo.so_id = so_id;
    sodbo = [sodbo get_sales_order_by_so_id];
    ItemDBO *idbo = [[ItemDBO alloc] init];
    
    svc.so_id=sodbo.so_id;
    svc.barcode.text=@"";
    svc.product_name.text=@"Product Name";
    svc.price.text=@"$ 0";
    svc.quantity.text=@"X 0";
    svc.unit_display.text=@"CASE";
    svc.product_package.text=@"Package Size : ";
    svc.total_amount = 0;
    svc.new_client_flag = FALSE;
    svc.sv_item_list=[[NSMutableArray alloc] init];
    svc.title = sodbo.client;
    svc.sv_item_list = [idbo load_item_list_from_so:sodbo.so_id];
    self.tabBarController.selectedIndex = 0;
//    [self.navigationController popViewControllerAnimated:YES];
    
}
- (void)update_remark:(id)sender{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @""
                                                   message: @"Update Remark for Sales Order"
                                                  delegate: self
                                         cancelButtonTitle:@"Cancel"
                                         otherButtonTitles:@"Confirm",nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    alert.tag = 2;
    [alert show];
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    //Already Posted to Server
    if (alertView.tag == 1) {
        if (buttonIndex == 0) {
            //cancell
        }else if(buttonIndex == 1){
            //ok to reposted
            ServiceConnector *sc = [[ServiceConnector alloc] init];
            [sc post_sales_order_item_list:so_id];
        }
        //update remark
    }else if (alertView.tag == 2) {
        if (buttonIndex == 0) {
            //cancell
        }else if(buttonIndex == 1){
            //ok
            remark = [alertView textFieldAtIndex:0].text;
            [self.tableView reloadData];
            SalesOrderDBO *sodbo = [[SalesOrderDBO alloc] init];
            sodbo.so_id = so_id;
            sodbo = sodbo.get_sales_order_by_so_id;
            sodbo.remark = [alertView textFieldAtIndex:0].text;
            [sodbo update_sales_order];
            
        }
    }
}

@end
